<?php

namespace app\api\service\jingdong;

use app\lib\jingdong\Jing;
use app\lib\jingdong\jingdongSdk\jd\JdClient;
use app\lib\jingdong\jingdongSdk\jd\request\CtpOrderCancelOrderRequest;
use app\lib\jingdong\jingdongSdk\jd\request\CtpOrderConfirmDeliveryRequest;
use app\lib\jingdong\jingdongSdk\jd\request\CtpOrderSubmitOrderRequest;


class JingDongOrderService extends Jing
{

    /**
     * @var JingDongOrderService
     */
    private static $instance;


    public static function getInstance(): JingDongOrderService
    {
        if (!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;
    }



    /**
     * 开普勒-订单创建
     */
    public function submitOrder($attrs =   [])
    {
        $c = new JdClient();
        $c->accessToken = $this->access_token();

        $protocol    =   array(
            "customerId"=>$c->customerId,
            "channelId"=>$c->channelId,
            "appKey"=>$c->appKey
        );
        //  发票信息 ： 2 增票 3 电子发票
        if($attrs['invoiceType'] ==  2)
        {
            $invoice =   array(
                //发票类型。2：增票，3：电子票 （如果选择开票此项必传）
                "invoiceType"=>2,
                "vatInvoice"=>array(
                    //增票资质公司名称
                    "companyName"=>"福州五光十色电子商务有限公司",
                    //增票资质公司纳税人标识号
                    "code"=>"91350102MA2YR0HP18",
                    //增票资质公司注册地址
                    "regAddr"=>"福建省福州市鼓楼区建华支巷水涧新村6座附属楼二层2F-209室（商务托管）",
                    //增票资质公司注册电话
                    "regPhone"=>"0591-83058931",
                    //增票资质公司注册银行
                    "regBank"=>"招商银行福州东街口支行",
                    //增票资质公司注册银行帐号
                    "regBankAccount"=>"591905723810902",
                    //增票邮寄收件人姓名
                    "consigneeName"=>"颜",
                    //增票邮寄收件人电话
                    "consigneeMobile"=>"18650363227",
                ),
            );
        }else{
            $invoice =   array(
                //发票类型。2：增票，3：电子票 （如果选择开票此项必传）
                "invoiceType"=>3,
                "electronicInvoice"=>array(
                    //发票抬头 。 4：个人 5：公司
                    "selectedInvoiceTitle"=>4,
                    //联系电话
                    "invoiceConsigneePhone"=>"18650363227"
                ),
            );
        }
        $param    =   array(
            //渠道订单号（抖音）
            "channelOrderId"=>"CSPOP0001",
            //代下单账号pin
            "pin"=>"jd_wgss",
            //商品总金额
            "orderFee"=>8.5,
            //运费金额 单位：元
            "freightFee"=>0,
            //支付方式。固定传2：在线支付
            "paymentType"=>2,
            //配送方式。1：京东配送 2：京配转三方配送 3：第三方配送 4：普通快递配送
            "shipmentType"=>4,
            //订单来源。 JD：京东，DD：当当，PP：拍拍，QQ：QQ网购，SN：苏宁，GM：国美，WPH：唯品会，1688：阿里巴巴，POS：POS门店，TB：淘宝，TM：天猫，KS：快手，OTHER：其他
            "channelOrderSource"=>"OTHER",
            //物品类型。固定传1
            "sendGoods"=>1,
            //用户IP地址
            "userIp"=>"117.27.75.13",
            //商品信息列表
            "productList"=>array([
                "mainSku"=>array(
                    //京东商品编号
                    "skuId"=>"851509",
                    //商品单价，单位：元
                    "skuPrice"=>8.5,
                    //商品数量
                    "quantity"=>1,
                    //商品名（测试商品，请勿下单 红色）
                    "skuName"=>"黑人牙膏",
                )
            ]),
            //收货地址信息
            "address"=>array(
                //京东标准省ID，若使用国标地址固定传0
                "provinceId"=>0,
                //京东标准市ID，若使用国标地址固定传0
                "cityId"=>0,
                "countyId"=>0,
                "townId"=>0,
                //详细全地址信息，必须传入
                "fullAddress"=>"福建省福州市仓山区建新镇亿恒创意广场",

            ),
            //收货人信息
            "receiver"=>array(
                //收货人姓名
                "receiverName"=>"颜",
                //收货人手机号
                "receiverMobile"=>"18650363227",
                //收货人邮箱
                "receiverEmail"=>"714257905@qq.com",
                //收货地邮政编码
                "zipCode"=>"350500"
            ),
            //发票信息
            "invoice"=>$invoice,
            //平台结算，渠道配置如无模式，不能传入
            "platformSettle"=>array(
                "settleModel"=>3,
                "commissionList"=>array([
                    "subCommissionId"=>"123",
                    "subCommissionName"=>"123",
                    "subCommissionFee"=>0,
                    "subCommissionRatio"=>0
                ]),
                "subsidyList"=>array([
                    "subsidyFee"=>0,
                    "subsidyRatio"=>0,
                    "subsidyId"=>""
                ]),
                "platformPointsFee"=>0,
                "platformPointsRatio"=>0
            )
        );


        $req = new CtpOrderSubmitOrderRequest();

        $req->setParam($param);
        $req->setProtocol($protocol);

        $resp = $c->execute($req, $c->accessToken);

        return $resp;
    }

    /**
     * 订单取消
     * */
    public function cancelOrder($attrs  =   [])
    {
        $c = new JdClient();
        $c->accessToken = $this->access_token();

        $req = new CtpOrderCancelOrderRequest();

        $req->setAppKey($c->appKey);
        //取消原因。1：订单不能按预计时间送达 2：其他渠道价格更低 3：该商品降价了 4：不想买了 5：其他原因 6：操作有误（商品、地址等选错） 7：商品无货 100：其他
        $req->setCancelReasonCode(5);
        $req->setPin($c->pin);
        $req->setOrderId($attrs['orderId']);
        $req->setChannelId($c->channelId);
        $req->setCustomerId($c->customerId);

        $resp = $c->execute($req, $c->accessToken);

        return $resp;
    }

    /**
     * 订单确认收获
     * */
    public function confirmDelivery($attrs  =   [])
    {
        $c=  new JdClient();
        $c->accessToken =   $this->access_token();

        $req    =   new CtpOrderConfirmDeliveryRequest();

        $req->setCustomerId($c->customerId);
        $req->setChannelId($c->channelId);
        $req->setOrderId($attrs['orderId']);
        $req->setAppKey($c->appKey);
        $req->setClientPort("8080");
        $req->setClientIp("127.0.0.1");

        $resp = $c->execute($req, $c->accessToken);

        return $resp;
    }




}